package com.example.myapplication5.ui.nav

import android.util.Log
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.navigation.compose.composable
import com.example.myapplication5.ui.screens.EditorScreen
import com.example.myapplication5.ui.screens.ListScreen
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import java.net.URLDecoder
import java.nio.charset.StandardCharsets

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun NavHostApp() {
    val navController = rememberAnimatedNavController()

    AnimatedNavHost(navController = navController, startDestination = Destinations.List.route) {
        //列表页
//        composable(Destinations.List.route){
        composable(Destinations.List.route) {
            ListScreen {
//                Editor$/5
//                Editor$/
                navController.navigate(Destinations.Editor(it).route)
            }
        }
        //详情或编辑页面  Editor$/id  "{${Params.Id.key}}" = {id}
        composable(Destinations.Editor("{${Params.Id.key}}").route) { backStackEntry ->
            val id = backStackEntry.arguments?.getString(Params.Id.key) ?: ""
            EditorScreen(id) {
                navController.popBackStack()
            }
        }
    }
}

sealed class Destinations(val route: String) {
    //列表页
    object List : Destinations("List")

    //详情页
    //传递参数 ？ 可选 可空
    //        / 必选
    data class Editor(val id: String) : Destinations("Editor?$id")
}

sealed class Params(val key: String) {
    object Id : Params("id")
}
